<?php
namespace clt;
use think\Exception;
class Sms {
    public static $timeout = 300;
    public static $url = 'http://service.winic.org/sys_port/gateway/?id=13265996244&pwd=x292971';
    public static function send($moblie=null,$type='login'){
        $yzm = mt_rand(1000,9999);//获取随机验证码
        $msg = "您的验证码是[".$yzm."]，5分钟秒内有效。请妥善保管,切勿告知他人。【立马付钱包】";
        $msg =iconv("utf-8","gbk",$msg);//这里需要转换成gbk
        $msg =urlencode($msg);
        $res = \clt\Curl::_get(self::$url."&to=".$moblie."&content=".$content);
        if($res == 0){
            db("msn_msg")->insert(
                array(
                    'phone' => $moblie,
                    'content' => $content,
                    'sendtime' => time(),
                    'yzm' => $yzm,
                    'status' => 0,
                    'type' => $type
                )
            );
            //发送成功
            return true;
        }
        throw new Exception("验证码发送失败", -1);
        return false;
    }
    /**验证验证码是否有效 */
    public static function check($moblie=null,$yzm=null){
        $sms = db('msn_msg')->where(['phone'=>$moblie,'yzm'=>$yzm])->find();
        if(!$sms){ //验证码不存在
            throw new Exception("验证码无效", -1);
            return false;
        }
        if($sms['status'] == 1 || $sms['sendtime'] < time()-self::$timeout){
            throw new Exception("验证码已失效", -1);
            return false;
        }
        return true;
    }
}